package model;

import init.DBManagement;

import java.io.IOException;
import java.util.*;
import model.beans.DocumentoBean;

public class Document {
	
	/**
	 * 
	 * @param int_minuta
	 * @return
	 */
	public static ArrayList list(int int_minuta){
		
		ArrayList arrL_list = new ArrayList(); 
		DBManagement db = new DBManagement();
		
		String str_query = 
			" SELECT DOC_TECH_KEY,                 " +
			"		 DOC_DESC                       " +
			" FROM   ACAMDOR                         " +
			" WHERE  MIN_TECH_KEY = "+int_minuta+"  ";
		
		db.executeQuery(str_query);
		
		int int_row  =  db.getNumRows();
		
		String str_descripcion = null;
		String str_documento   = null;
		
		for(int int_i= 0 ; int_i<int_row ;int_i++){
			DocumentoBean bn_documento = new DocumentoBean();
			str_documento   = db.getItem(int_i, 0);
			str_descripcion = db.getItem(int_i, 1);
			
			bn_documento.setInt_minuta(int_minuta);
			bn_documento.setInt_documento(Integer.parseInt(str_documento));
			bn_documento.setStr_descripcion(str_descripcion);
			
			arrL_list.add(bn_documento);
		}
		DBManagement.closeConnection();
		
		
		return arrL_list;
	}
	
	
	/**
	 * Elimina documento
	 * 
	 * @param int_documento
	 * @return
	 */
	public static void delete(int int_documento){
		DBManagement db = new DBManagement();
		
		String str_query = 
			" DELETE FROM ACAMDOR " +
			" WHERE DOC_TECH_KEY = "+int_documento+" ";
		db.executeQuery(str_query);
		DBManagement.closeConnection();
	}
	
	
	/**
	 * 
	 * @param int_documento
	 * @throws SQLException 
	 * @throws IOException 
	 */
	public static String getDocument(int int_documento, String str_path) throws Exception{
		DBManagement db = new DBManagement();
		String str_createdFile = db.getFile("ACAMDOR", "DOC_TECH_KEY", "DOCUMENTO", int_documento, str_path, "NOMBRE_DOC");		
		return str_createdFile;
	}
	
	
	/**
	 * 
	 * @param map_add
	 * @throws Exception 
	 */
	public static void add(Map map_add) throws Exception{
		int int_minuta  = ((Integer)map_add.get("int_minuta")).intValue();
		String str_descripcion = (String)map_add.get("str_descripcion");
		byte[] byte_documento    = ((byte[])map_add.get("byte_documento"));
		String str_nombre = (String)map_add.get("nombre_documento");
		
		DBManagement db = new DBManagement();
		
		String str_insert = 
			" INSERT INTO ACAMDOR(            " +
			"        MIN_TECH_KEY,            " +
			"        DOC_DESC,                " +
			"        ULTIMA_MOD,              " +
			"        NOMBRE_DOC)              " +
			"	VALUES(                       " +
			"                                 " +
			"          "+int_minuta+",        " +
			"          '"+str_descripcion+"', " +
			"           SYSDATE,              " +
			"          '"+str_nombre+"'       " +
			"		    )                     ";
		
		db.executeQuery(str_insert);
		DBManagement.closeConnection();
		
		db = new DBManagement();
		int int_idDocumento = 0;
		String str_query = 
			" SELECT max( DOC_TECH_KEY ) " +
			" FROM   ACAMDOR             " +
			" WHERE  MIN_TECH_KEY =      "+int_minuta;
		
		db.executeQuery(str_query);
		int_idDocumento = Integer.parseInt(db.getItem(0, 0));
		db.uploadBlob("ACAMDOR", "DOC_TECH_KEY", "DOCUMENTO", byte_documento, int_idDocumento);
		DBManagement.closeConnection();
	}
}
